//
//  DiseaseViewController.m
//  project_delicious
//
//  Created by dlios on 15-3-24.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DiseaseViewController.h"
#import "FoodTherapyCell.h"
#import "DiseaseDetailViewController.h"
#import "DisCollectViewController.h"
#import "UIColor+AddColor.h"

@interface DiseaseViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) MBProgressHUD *hud;
@end

@implementation DiseaseViewController
- (void)dealloc
{
    [_hud release];
    [_str release];
    [_arr release];
    [super dealloc];
}
- (NSURL *)strChangeUrl:(NSString *)str
{
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    return url;
}
//转菊花
- (void)mbHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    self.hud.labelText = @"Loading...";
    //self.hud.detailsLabelText = @"正在为您加载";
    [self.view addSubview:self.hud];
    [_hud release];
    [_hud show:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self mbHud];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"QQ20150406-1.png"]];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:self.str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arr = [responseObject objectForKey:@"data"];
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor clearColor];
        //tableView.separatorColor = [UIColor clearColor];
        [self.view addSubview:tableView];
        [tableView release];
        [tableView registerClass:[FoodTherapyCell class] forCellReuseIdentifier:@"guo"];
        [_hud removeFromSuperview];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"发生错误:%@", error);
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodTherapyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"guo"];
    NSString *str = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"imageName"];
//    NSLog(@"---%@", str);
    [cell.picImageView setImageWithURL:[self strChangeUrl:str]];
    cell.picImageView.contentMode = UIViewContentModeScaleAspectFit;
    cell.mainLabel.layer.cornerRadius = 5;
    cell.mainLabel.layer.borderWidth = 0.5;
    cell.mainLabel.textAlignment = NSTextAlignmentCenter;
    cell.mainLabel.backgroundColor = [UIColor colorFromHexCode:@"ffce7b"];
    cell.mainLabel.layer.masksToBounds = YES;
    cell.mainLabel.text = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"diseaseName"];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 8;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DisCollectViewController *collect = [[DisCollectViewController alloc] init];
    
    NSString *strId = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"diseaseId"];
    
    NSString *reStrP = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tbldisease!getVegetable.do?diseaseId=%@&page=", strId];
    NSString *reStrS = @"&pageRecord=10&phonetype=0&is_traditional=0";
    collect.reStrP = reStrP;
    collect.reStrS = reStrS;
    
    NSString *preStr1 = @"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tbldisease!getVegetable.do?diseaseId=";
    NSString *suffStr1 = @"&page=1&pageRecord=10&phonetype=0&is_traditional=0";
    NSString *str = [[preStr1 stringByAppendingString: strId] stringByAppendingString:suffStr1];
    
    //
    NSString *preStr = @"http://121.41.87.202:80/HandheldKitchen/api/vegetable/tblvegetable!getTblVegetables.do?vegetable_id=";
    NSString *suffStr = @"&phonetype=2&user_id=&is_traditional=0";
    collect.preStr = preStr;
    collect.suffStr = suffStr;
    //    NSLog(@"1111111111111111111%@", str);
    collect.str = str;
    
    NSString *preStrMat = @"http://121.41.87.202:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=";
    NSString *suffStrMat = @"&type=1&phonetype=0&is_traditional=0";
    
    collect.preStrMat = preStrMat;
    collect.suffStrMat = suffStrMat;
    
    NSString *preStrCook = @"http://121.41.87.202:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=";
    NSString *suffStrCook = @"&type=2&phonetype=0&is_traditional=0";
    collect.preStrCook = preStrCook;
    collect.suffStrCook = suffStrCook;
    
    collect.dic = [self.arr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:collect animated:YES];
    [collect release];


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
